// manager/pages/merchantdesc/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        type: "",
        // 行业字典
        industryList: [],
        // 业务字典
        // busiList: [],
        openList: [],
        keynoteList: [],
        // 商户标签
        merchantTag: [],
        // 商户信息
        merchantId: "",
        merchantDetail: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            merchantId: options.merchantId,
            type: options.type
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getMerchantInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },


    /**
     * 修改信息
     */
    editForm(){
        wx.navigateTo({
          url: `/manager/pages/editmerchant/index?merchantId=${this.data.merchantId}`,
        })
    },

    // 商户信息
    getMerchantInfo(){
        request({
            url: "/merchantMarketing/getMerchantInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: this.data.merchantId
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    merchantDetail: res.data
                })
                // 获取字典
                this.getDictionaryInfo();
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    // 查看图片
    previewImage(e) {
        let { url } = e.currentTarget.dataset;
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
        })
    },

    /**
     * 获取所属行业、营销重点字典
     */
    getDictionaryInfo() {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
				dictType: "ecard_merchant_industry"
			}
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    industryList: res.data
                })
            }
            return request({
                url: "/commonApi/getDictionaryInfo",
                method: "GET",
                data: {
                    dictType: "ecard_merchant_business_tag"
                }
            })
        }).then(res => {
            if(res.code == 0){
                // this.setData({
                //     busiList: res.data
                // })
                this.getOpenAndKeynoteList(res.data);
            }
            return request({
                url: "/commonApi/getDictionaryInfo",
                method: "GET",
                data: {
                    dictType: "ecard_merchant_tag"
                }
            })
        }).then(res => {
            if(res.code == 0){
                // this.setData({
                //     busiList: res.data
                // })
                this.getMerchantTag(res.data);
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    // 获取已开通、营销重点
    getOpenAndKeynoteList(list){
        let openList = [];
        let keynoteList = [];
        let barr = this.data.merchantDetail.businessLabel?this.data.merchantDetail.businessLabel.split(","):[];
        let farr = this.data.merchantDetail.marketingFocus?this.data.merchantDetail.marketingFocus.split(","):[];
        list.forEach(item => {
            if(barr.indexOf(item.dictValue)!=-1){
                openList.push(item)
            }
            if(farr.indexOf(item.dictValue)!=-1){
                keynoteList.push(item)
            }
        })
        this.setData({
            openList,
            keynoteList
        })
    },
    // 获取商户标签
    getMerchantTag(list){
        let merchantTag = [];
        let arr = this.data.merchantDetail.merchantLabel?this.data.merchantDetail.merchantLabel.split(","):[];
        list.forEach(item => {
            if(arr.indexOf(item.dictValue)!=-1){
                merchantTag.push(item)
            }
        })
        this.setData({
            merchantTag
        })
    },
})